package com.xcity.game.achievement;

import com.xcity.game.item.ItemInfo;

import naga.x.game.template.Template;
import naga.x.game.template.TemplateException;
import naga.x.game.template.annotation.Excel;

/**
 * 成就
 * @author yang.li
 *
 */
@Excel(value = "achievement.xlsx", sheet = 0)
public class AchievementTemplate extends Template {

	/** 成就类型(详见第二个sheet) */
	protected AchievementGroup group;

	/** 成就名 */
	protected String name;

	/** 描述 */
	protected String desc;

	/** 图标 */
	protected String icon;

	/** 奖励金币 */
	protected int gold;

	/** 奖励钻石 */
	protected int stone;

	/** 奖励经验 */
	protected int exp;
	
	/** 奖励物品 */
	protected int[] items;
	
	protected ItemInfo[] rewardItems;

	/** 完成条件 */
	protected long[] conditions;

	public AchievementGroup getGroup() {
		return group;
	}

	public void setGroup(AchievementGroup group) {
		this.group = group;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	public int getGold() {
		return gold;
	}

	public void setGold(int gold) {
		this.gold = gold;
	}

	public int getStone() {
		return stone;
	}

	public void setStone(int stone) {
		this.stone = stone;
	}

	public int getExp() {
		return exp;
	}

	public void setExp(int exp) {
		this.exp = exp;
	}

	public int[] getItems() {
		return items;
	}

	public void setItems(int[] items) {
		this.items = items;
	}

	public long[] getConditions() {
		return conditions;
	}

	public void setConditions(long[] conditions) {
		this.conditions = conditions;
	}
	
	public long getCondition(int index) {
		if (index >= 0 && index < conditions.length) {
			return conditions[index];
		}
		return 0;
	}
	
	public ItemInfo[] getRewardItems() {
		return rewardItems;
	}

	@Override
	public void validate() throws TemplateException {
		if (group == null || group == AchievementGroup.UNKNOWN) {
			throw new TemplateException("成就配置错误, id = " + id + ", group = " + group);
		}
		int len;
		if (items != null && (len = items.length) > 0) {
			rewardItems = new ItemInfo[len >> 1];
			for (int i = 0, pos = 0; i < len; i += 2) {
				rewardItems[pos++] = new ItemInfo(items[i], items[i + 1]);
			}
			items = null; // gc
		}
	}
}
